<template>
  <div class="panel">
    <div class="chart-container">
      <div ref="pieChartRef" class="pie-chart"></div>
      <div ref="barChartRef" class="bar-chart"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'

const pieChartRef = ref<HTMLElement | null>(null)
const barChartRef = ref<HTMLElement | null>(null)
let pieChart: echarts.ECharts | null = null
let barChart: echarts.ECharts | null = null

const initCharts = () => {
  if (!pieChartRef.value || !barChartRef.value) return
  
  // 初始化饼图
  pieChart = echarts.init(pieChartRef.value)
  const pieOption = {
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c}%'
    },
    series: [
      {
        type: 'pie',
        radius: ['30%', '80%'],
        center: ['50%', '50%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderWidth: 0
        },
        label: {
          show: true,
          position: 'inside',
          formatter: '{c}%',
          fontSize: 12,
          color: '#fff',
          align: 'center',
          verticalAlign: 'middle'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 13,
            fontWeight: 'bold',
            color: '#fff'
          }
        },
        data: [
          { value: 35, name: '砂土', itemStyle: { color: '#33ffd0' } },
          { value: 25, name: '粘土', itemStyle: { color: '#F56C6C' } },
          { value: 20, name: '壤土', itemStyle: { color: '#E6A23C' } },
          { value: 15, name: '粉土', itemStyle: { color: '#909399' } },
          { value: 5, name: '其他', itemStyle: { color: '#409EFF' } }
        ]
      }
    ]
  }
  
  // 初始化柱状图
  barChart = echarts.init(barChartRef.value)
  const barOption = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: '10%',
      containLabel: true
    },
    xAxis: {
      type: 'value',
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: '#33ffd0'
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(51, 255, 208, 0.1)'
        }
      }
    },
    yAxis: {
      type: 'category',
      data: ['pH值', '盐分', '有机质', '养分'],
      axisLine: {
        lineStyle: {
          color: '#33ffd0'
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: '#33ffd0'
      }
    },
    series: [
      {
        type: 'bar',
        data: [
          {
            value: 6.5,
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                { offset: 0, color: '#33ffd0' },
                { offset: 1, color: 'rgba(51, 255, 208, 0.3)' }
              ])
            }
          },
          {
            value: 0.8,
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                { offset: 0, color: '#F56C6C' },
                { offset: 1, color: 'rgba(245, 108, 108, 0.3)' }
              ])
            }
          },
          {
            value: 2.5,
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                { offset: 0, color: '#E6A23C' },
                { offset: 1, color: 'rgba(230, 162, 60, 0.3)' }
              ])
            }
          },
          {
            value: 3.2,
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                { offset: 0, color: '#409EFF' },
                { offset: 1, color: 'rgba(64, 158, 255, 0.3)' }
              ])
            }
          }
        ],
        barWidth: '60%',
        itemStyle: {
          borderRadius: [0, 20, 20, 0]
        }
      }
    ]
  }
  
  pieChart.setOption(pieOption)
  barChart.setOption(barOption)
}

// 监听窗口大小变化
const handleResize = () => {
  pieChart?.resize()
  barChart?.resize()
}

onMounted(() => {
  initCharts()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  pieChart?.dispose()
  barChart?.dispose()
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.panel {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart-container {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
}

.pie-chart {
  width: 50%;
  height: 100%;
}

.bar-chart {
  width: 50%;
  height: 100%;
}
</style> 